package cn.dcpnet.manager;

import cn.dcpnet.DFeignRequestManager;
import cn.dcpnet.IDFeignRequest;
import cn.dcpnet.IDFeignResponse;
import cn.dcpnet.pojo.ResponseDto;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author: 代国平
 * @since: 2025/1/7 16:45
 */
@Component
public class HttpRequestManager implements DFeignRequestManager {

    @Resource
    private RestTemplate restTemplate;

    /**
     * 发送请求
     *
     * @param deviceCode 设备编码
     * @param module     模块
     * @param api        接口
     * @param token      token
     * @param timeout    超时时间
     * @param data       请求报文
     * @param params     请求参数
     * @return 响应报文
     */
    public IDFeignResponse<?> sendMessage(String deviceCode, String module, String api,
                                          String token, Long timeout, IDFeignRequest<?> data, Object... params) {
        // 集群内查找设备，直接拼接设备号到 URL
        ResponseEntity<ResponseDto> entity = restTemplate.postForEntity("http://localhost:8088/" + module + "/" + api, data, ResponseDto.class);
        ResponseDto<?> responseDto = entity.getBody();
        return responseDto;
    }
}
